package com.feicheng.iou.controller;


import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.Backview;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.BackviewUpdatePOJO;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.pojo.login.PermissionLevelTwoPOJO;
import com.feicheng.iou.pojo.login.PermissionListPOJO;
import com.feicheng.iou.pojo.resourceBackviewParamPOJO.BackviewListParamPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.BackviewService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/backview")
@Slf4j
public class BackviewController {

    @Autowired
    private BackviewService backviewService; //回访业务的实现类

    /**
     * @Author wuweinan
     * @Description 添加回访信息接口
     * @Date 2021-6-29 17:19
     * @param backview
     * @return ResultInfo
     */
    @PostMapping
    public ResultInfo addBackview(Backview backview) {

        log.info("BackviewController的addBackview方法进入");

        //判断请求是否重复
        if (!IdempotentUtils.judge(backview, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"新增回访"));
        }

        //校验参数是否为空
        if (backview == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        log.info("BackviewController的addBackview方法返回");
        return backviewService.insertBackview(backview);
    }

    /**
     * @param backviewListParamPOJO
     * @return ResultInfo
     * @Author wuweinan
     * @Description 查询回访列表接口
     * @Date 2021-6-29 17:19
     */
    @GetMapping("/list")
    public ResultInfo queryBackviewListPagesByConditions(BackviewListParamPOJO backviewListParamPOJO, HttpServletRequest request) {
        log.info("BackviewController的queryBackviewListPagesByConditions方法进入");

        Integer distributionCount = 0; //统计权限中可分配的个数 其实就是有和没有

        //检验参数是否为空
        if (backviewListParamPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //查询回访列表接口
        log.info("BackviewController的queryBackviewListPagesByConditions方法返回");

        LoginReturnPOJO loginReturnPOJO =
                (LoginReturnPOJO)request.getSession().getAttribute(SessionConstant.CURRENT);

        //判断是否有可分配权限
        for (PermissionListPOJO permission : loginReturnPOJO.getPermissions()) {
            for (PermissionLevelTwoPOJO permissionPermission : permission.getPermissions()) {
                if(permissionPermission.getName().equals("可分配")){
                    distributionCount++;
                }
            }
        }


        if(distributionCount < 1){     //没有"可分配" 权限的人只能看自己的backview
            //直接写死了传也没有用
            Backview backview = new Backview();
            backview.setAutor(loginReturnPOJO.getUser().getUserId());
            backviewListParamPOJO.setBackview(backview);
        }
        return backviewService.selectBackviewsPagesByConditions(backviewListParamPOJO);
    }



    /**
     * @param backviewListParamPOJO
     * @return ResultInfo
     * @Author wuweinan
     * @Description 查询单个线索接口
     * @Date 2021-6-29 17:19
     */
    @GetMapping("/info")
    public ResultInfo queryBackviewPagesByConditions(BackviewListParamPOJO backviewListParamPOJO) {
        log.info("BackviewController的queryBackviewPagesByConditions方法进入");

        //检验参数是否为空
        if (backviewListParamPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        log.info("BackviewController的queryBackviewPagesByConditions方法返回");
        return backviewService.selectBackviewPagesByConditions(backviewListParamPOJO);
    }

    /**
     * @param backviewUpdatePOJO
     * @return ResultInfo
     * @Author wuweinan
     * @Description 更新回访信息接口
     * @Date 2021-6-29 17:19
     */
    @PutMapping
    public ResultInfo updateBackviewById(BackviewUpdatePOJO backviewUpdatePOJO) {
        log.info("BackviewController的updateBackviewById方法进入");

       //检验参数是否为空
        if (backviewUpdatePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //根据BackviewId更新一条Backview信息.
        log.info("BackviewController的updateBackviewById方法返回");
        return backviewService.updateBackviewById(backviewUpdatePOJO);
    }
}

